package org.example.iris.controller;

import org.example.iris.entity.Application;
import org.example.iris.dto.Result;
import org.example.iris.dto.ApplicationInfo;
import org.example.iris.service.impl.ApplicationServiceImpl;
import org.example.iris.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/applications")
public class ApplicationController {

    @Autowired
    private ApplicationServiceImpl applicationService;

    /**
     * 更新申请状态
     * @param applicationId 申请ID
     * @param status 新的状态
     * @return 更新结果
     */
    @PostMapping("/approve")
    public String approveApplication(@RequestParam int applicationId, @RequestParam String status) {
        // 调用服务层方法更新申请状态
        applicationService.approveApplication(applicationId, status);
        return "申请状态更新成功";
    }

    /**
     * 获取某个岗位的所有申请ID
     * @param jobId 岗位ID
     * @return 申请ID列表
     */
    @GetMapping("/getApproveList/{jobId}")
    public List<ApplicationInfo> getApplicationIdsAndStudentIdsByJobId(@PathVariable int jobId) {
        // 调用服务层获取申请ID和学生ID
        return applicationService.getApplicationIdsAndStudentIdsByJobId(jobId);
    }

    @GetMapping("getApplication")
    public Result<List<Application>> getApplication() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");

        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        List<Application> applications = applicationService.getApplication(userId);
        return Result.success(applications);
    }

    @DeleteMapping("/deleteApplication")
    public Result deleteApplication(@RequestBody Map<String, Integer> params) {
        applicationService.deleteApplication(params.get("applicationId"));
        return Result.success();
    }
}
